package com.example.demo.task;

import com.example.demo.mapper.InviteCodeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * 邀请码定时任务处理类
 */
@Component
public class InviteCodeScheduled {
    private final InviteCodeMapper inviteCodeMapper;

    @Autowired
    public InviteCodeScheduled(InviteCodeMapper inviteCodeMapper) {
        this.inviteCodeMapper = inviteCodeMapper;
    }

    /**
     * 定时任务：每天删除七天前已使用的邀请码
     */
    @Transactional
    @Scheduled(cron = "0 0 1 * * ?") // 每天凌晨1点执行
    public void deleteOldUsedInviteCodes() {
        LocalDateTime sevenDaysAgo = LocalDateTime.now().minusDays(7);
        inviteCodeMapper.deleteUsedCodesOlderThan(sevenDaysAgo);
    }
}